package com.example.exp1;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class ReservationSystem {
    private List<MeetingRoom> meetingRooms; // 会议室信息
    private List<Reservation> reservations; // 预定信息

    public ReservationSystem(List<MeetingRoom> meetingRooms) {
        this.meetingRooms = meetingRooms;
        this.reservations = new ArrayList<>();
    }

    // 查看所有会议室
    public void showAllMeetingRooms() {
        System.out.println("---所有会议室列表---");
        System.out.println("名称       容纳人数     多媒体设备");
        for (MeetingRoom meetingRoom : meetingRooms) {
            System.out.println(meetingRoom);
        }
        System.out.println("-------------");
    }

    // 预定会议室
    public void reserveMeetingRoom() {
        Scanner sc = new Scanner(System.in);

        // 遍历可用会议室
        for (int i = 0; i < meetingRooms.size(); i++) {
            MeetingRoom room = meetingRooms.get(i);
            System.out.println((i + 1) + ". " + room.getName()
                    + "（容纳人数：" + room.getCapacity() + ", 多媒体设备："
                    + (room.hasMedia() ? "有" : "无") + ")");
        }

        // 选择会议室
        System.out.print("请选择要预定的会议室: ");
        int roomNo = sc.nextInt();
        MeetingRoom selectedRoom = meetingRooms.get(roomNo - 1);

        // 选择预定日期
        System.out.print("已选择" + selectedRoom.getName() + ",");
        System.out.print("请选择预定日期(yyyy-MM-dd): ");
        String dateStr = sc.next();
        LocalDate date = LocalDate.parse(dateStr, java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        // 选择预定时间段
        System.out.print("请选择预定时间段（例如：10:00-11:00）: ");
        String timeStr = sc.next();
        String[] time = timeStr.split("-");
        LocalTime startTime = LocalTime.parse(time[0], java.time.format.DateTimeFormatter.ofPattern("HH:mm"));
        LocalTime endTime = LocalTime.parse(time[1], java.time.format.DateTimeFormatter.ofPattern("HH:mm"));

        // 判断该会议室该时间段是否被预定
        if (isAvailable(selectedRoom, date, startTime, endTime)) {  // 未被预定
            // 创建预定信息
            Reservation reservation = new Reservation(selectedRoom, date, startTime, endTime);
            // 加入到预定集合中
            reservations.add(reservation);
            System.out.println("预定成功");
        } else {
            System.out.println("该时间段会议室已被预定，请选择其他时间段或会议室");
        }
    }

    // 判断该会议室该时间段是否被预定
    public boolean isAvailable(MeetingRoom selectedRoom, LocalDate date, LocalTime startTime, LocalTime endTime) {
        List<Reservation> reservationList = reservations.stream()
                .filter(r -> r.getMeetingRoom().equals(selectedRoom))
                .collect(Collectors.toList());

        if (reservationList.isEmpty()) {
            return true;
        }

        for (Reservation r : reservationList) {
            if (date.equals(r.getDate())) {
                if (startTime.isBefore(r.getEndTime()) && endTime.isAfter(r.getStartTime())) {
                    return false;
                }
            }
        }

        return true;
    }

    // 展示会议室预定情况
    public void showResInfo() {
        System.out.println("---所有预定信息---");
        for (MeetingRoom meetingRoom : meetingRooms) {
            System.out.println(meetingRoom.getName());
            List<Reservation> roomReservations = reservations.stream()
                    .filter(r -> r.getMeetingRoom().equals(meetingRoom))
                    .collect(Collectors.toList());
            for (Reservation reservation : roomReservations) {
                System.out.println(reservation);
            }
            if (roomReservations.isEmpty()) {
                System.out.println("该会议室无预定信息");
            }
        }
        System.out.println("------------");
    }

    // 查看某时间段内可用的会议室
    public void selectRoomByTime() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请按日期格式输入(yyyy-MM-dd): ");
        String dateStr = sc.next();
        LocalDate date = LocalDate.parse(dateStr, java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        System.out.print("请输入时间段(例如：09:00-10:00): ");
        String timeStr = sc.next();
        String[] time = timeStr.split("-");
        LocalTime startTime = LocalTime.parse(time[0], java.time.format.DateTimeFormatter.ofPattern("HH:mm"));
        LocalTime endTime = LocalTime.parse(time[1], java.time.format.DateTimeFormatter.ofPattern("HH:mm"));

        List<Reservation> conflictList = reservations.stream()
                .filter(r -> date.equals(r.getDate())
                        && startTime.isBefore(r.getEndTime())
                        && endTime.isAfter(r.getStartTime()))
                .collect(Collectors.toList());

        List<MeetingRoom> conflictRooms = conflictList.stream()
                .map(Reservation::getMeetingRoom)
                .collect(Collectors.toList());

        System.out.println("名称       容纳人数     多媒体设备");
        for (MeetingRoom meetingRoom : meetingRooms) {
            if (!conflictRooms.contains(meetingRoom)) {
                System.out.println(meetingRoom);
            }
        }
    }
}
